const util = require('../../utils/util')

Page({
  data: {
    // 选择的学校ID
    selectedSchool: '',
    
    // 学校列表
    schools: [
      { id: 1, name: '清华大学' },
      { id: 2, name: '北京大学' },
      { id: 3, name: '复旦大学' },
      { id: 4, name: '上海交通大学' },
    ],
    
    // 角色列表
    roles: [
      { id: 1, name: '普通用户', description: '可以浏览和购买商品' },
      { id: 2, name: '卖家', description: '可以发布和管理商品' },
      { id: 3, name: '管理员', description: '可以管理系统设置和用户' },
      { id: 4, name: '审核员', description: '可以审核商品和用户信息' },
    ],
    
    // 权限树
    permissions: [
      {
        id: 1,
        label: '用户管理',
        checked: false,
        children: [
          { id: 11, label: '查看用户', checked: false },
          { id: 12, label: '编辑用户', checked: false },
          { id: 13, label: '删除用户', checked: false },
        ]
      },
      {
        id: 2,
        label: '商品管理',
        checked: false,
        children: [
          { id: 21, label: '查看商品', checked: false },
          { id: 22, label: '发布商品', checked: false },
          { id: 23, label: '编辑商品', checked: false },
          { id: 24, label: '删除商品', checked: false },
        ]
      },
      {
        id: 3,
        label: '订单管理',
        checked: false,
        children: [
          { id: 31, label: '查看订单', checked: false },
          { id: 32, label: '处理订单', checked: false },
          { id: 33, label: '删除订单', checked: false },
        ]
      },
      {
        id: 4,
        label: '系统设置',
        checked: false,
        children: [
          { id: 41, label: '学校管理', checked: false },
          { id: 42, label: '角色管理', checked: false },
          { id: 43, label: '权限设置', checked: false },
        ]
      }
    ],
    
    // UI状态
    schoolIndex: -1,
    expandedPermissions: [1, 2, 3, 4] // 默认展开所有权限组
  },

  onLoad() {
    // 检查登录状态
    util.checkLoginStatus();
  },
  
  onShow() {
    // 更新tabBar状态 - 租户选项为索引4
    if (typeof this.getTabBar === 'function') {
      this.getTabBar().setData({
        selected: 4
      });
    }
  },
  
  // 返回首页
  goToHome() {
    wx.switchTab({
      url: '/pages/index/index'
    });
  },
  
  // 选择学校
  bindSchoolChange(e) {
    const index = e.detail.value;
    const school = this.data.schools[index];
    
    this.setData({
      schoolIndex: index,
      selectedSchool: school.id
    });
  },
  
  // 编辑角色权限
  editRolePermissions(e) {
    const roleId = e.currentTarget.dataset.id;
    
    wx.showToast({
      title: '编辑角色ID: ' + roleId,
      icon: 'none'
    });
  },
  
  // 切换权限选中状态
  togglePermission(e) {
    const { groupId, permissionId } = e.currentTarget.dataset;
    
    if (permissionId) {
      // 子权限
      const group = this.data.permissions.find(p => p.id === groupId);
      const permissionIndex = group.children.findIndex(p => p.id === permissionId);
      
      const key = `permissions[${this.data.permissions.findIndex(p => p.id === groupId)}].children[${permissionIndex}].checked`;
      const newValue = !group.children[permissionIndex].checked;
      
      this.setData({
        [key]: newValue
      });
    } else {
      // 权限组
      const groupIndex = this.data.permissions.findIndex(p => p.id === groupId);
      const group = this.data.permissions[groupIndex];
      const newValue = !group.checked;
      
      const updates = {};
      updates[`permissions[${groupIndex}].checked`] = newValue;
      
      // 同时更新所有子权限
      group.children.forEach((child, i) => {
        updates[`permissions[${groupIndex}].children[${i}].checked`] = newValue;
      });
      
      this.setData(updates);
    }
  },
  
  // 切换权限组展开状态
  toggleExpand(e) {
    const groupId = e.currentTarget.dataset.id;
    const expandedPermissions = [...this.data.expandedPermissions];
    const index = expandedPermissions.indexOf(groupId);
    
    if (index >= 0) {
      expandedPermissions.splice(index, 1);
    } else {
      expandedPermissions.push(groupId);
    }
    
    this.setData({
      expandedPermissions: expandedPermissions
    });
  }
}) 